﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类MainCommunity。
    /// </summary>
    public class MainCommunity
    {
        public MainCommunity()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from MainCommunity where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.MainCommunity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into MainCommunity(");
            strSql.Append("RecordId,CommunityId,CommunityName,SaleHouse,HireHouse,AveragePrice,UpDown,UpdateTime,AddPerson,States)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@CommunityId,@CommunityName,@SaleHouse,@HireHouse,@AveragePrice,@UpDown,@UpdateTime,@AddPerson,@States)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "CommunityId", DbType.Guid, model.CommunityId);
            db.AddInParameter(dbCommand, "CommunityName", DbType.AnsiString, model.CommunityName);
            db.AddInParameter(dbCommand, "SaleHouse", DbType.Double, model.SaleHouse);
            db.AddInParameter(dbCommand, "HireHouse", DbType.Double, model.HireHouse);
            db.AddInParameter(dbCommand, "AveragePrice", DbType.Currency, model.AveragePrice);
            db.AddInParameter(dbCommand, "UpDown", DbType.Decimal, model.UpDown);
            db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, model.UpdateTime);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "States", DbType.AnsiString, model.States);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.MainCommunity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update MainCommunity set ");
            strSql.Append("CommunityId=@CommunityId,");
            strSql.Append("CommunityName=@CommunityName,");
            strSql.Append("SaleHouse=@SaleHouse,");
            strSql.Append("HireHouse=@HireHouse,");
            strSql.Append("AveragePrice=@AveragePrice,");
            strSql.Append("UpDown=@UpDown,");
            strSql.Append("UpdateTime=@UpdateTime,");
            strSql.Append("AddPerson=@AddPerson,");
            strSql.Append("States=@States");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "CommunityId", DbType.Guid, model.CommunityId);
            db.AddInParameter(dbCommand, "CommunityName", DbType.AnsiString, model.CommunityName);
            db.AddInParameter(dbCommand, "SaleHouse", DbType.Double, model.SaleHouse);
            db.AddInParameter(dbCommand, "HireHouse", DbType.Double, model.HireHouse);
            db.AddInParameter(dbCommand, "AveragePrice", DbType.Currency, model.AveragePrice);
            db.AddInParameter(dbCommand, "UpDown", DbType.Decimal, model.UpDown);
            db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, model.UpdateTime);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "States", DbType.AnsiString, model.States);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from MainCommunity ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.MainCommunity GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,CommunityId,CommunityName,SaleHouse,HireHouse,AveragePrice,UpDown,UpdateTime,AddPerson,States from MainCommunity ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.House.Entity.MainCommunity model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,CommunityId,CommunityName,SaleHouse,HireHouse,AveragePrice,UpDown,UpdateTime,AddPerson,States ");
            strSql.Append(" FROM MainCommunity ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "MainCommunity");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.MainCommunity> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,CommunityId,CommunityName,SaleHouse,HireHouse,AveragePrice,UpDown,UpdateTime,AddPerson,States ");
            strSql.Append(" FROM MainCommunity ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.MainCommunity> list = new List<Chenxin.Lib.House.Entity.MainCommunity>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.MainCommunity ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.MainCommunity model = new Chenxin.Lib.House.Entity.MainCommunity();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["CommunityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CommunityId = new Guid(ojb.ToString());
            }
            model.CommunityName = dataReader["CommunityName"].ToString();
            ojb = dataReader["SaleHouse"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SaleHouse = (decimal)ojb;
            }
            ojb = dataReader["HireHouse"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.HireHouse = (decimal)ojb;
            }
            ojb = dataReader["AveragePrice"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AveragePrice = (decimal)ojb;
            }
            ojb = dataReader["UpDown"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UpDown = (decimal)ojb;
            }
            ojb = dataReader["UpdateTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UpdateTime = (DateTime)ojb;
            }
            model.AddPerson = dataReader["AddPerson"].ToString();
            model.States = dataReader["States"].ToString();
            return model;
        }

        #endregion  成员方法
    }
}

